package com.kun.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.rental.entity.CarrentalInfo;
import com.kun.rental.entity.Maintain;
import com.kun.rental.service.ICarrentalInfoService;
import com.kun.rental.service.IMaintainService;
import com.kun.rental.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 * 车辆保养管理
 *
 * @author ikun
 * @since 2024-07-04
 */
@RestController
@Api(tags = "车辆保养管理")
@RequestMapping("/rental/maintain")
public class MaintainController {

    @Autowired
    private IMaintainService maintainService;

    @Autowired
    private ICarrentalInfoService carrentalInfoService;


    /**
     * 分页查询
     * @param start
     * @param size
     * @param maintain
     * @return
     */
    @PostMapping("/search/{start}/{size}")
    public Result search(@PathVariable int start,
                         @PathVariable int size,
                         @RequestBody Maintain maintain){
        Page<Maintain> page = new Page<>(start, size);
        return Result.success(maintainService.searchByPage(page, maintain));
    }


    /**
     * 新增
     * @param maintain
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Maintain maintain) {
        //设置保养关联的车辆信息
        //设置保养关联的车辆信息
        CarrentalInfo autoInfo = carrentalInfoService.selectByCarrentalNum(maintain.getNum());
        maintain.setCarrentalId(autoInfo.getId());
        //将车辆实保次数+1
        autoInfo.setActualNum(autoInfo.getActualNum() + 1);
        carrentalInfoService.updateById(autoInfo);
        return maintainService.save(maintain)?Result.success():Result.fail();
    }


    /**
     * 修改
     * @param maintain
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Maintain maintain) {
        return maintainService.updateById(maintain)?Result.success():Result.fail();
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/{ids}")
    public Result delete(@PathVariable String ids){
        return maintainService.delete(ids)?Result.success():Result.fail();
    }

}
